local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local BarTaskExcel = require("excel.barTask")
local ItemExcel = require("excel.item")
local HeroExcel = require("excel.hero")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local ItemDefine = require("bag.ItemDefine")
local HeroDefine = require("hero.HeroDefine")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
local RoleExcel = require("excel.role")
local Util = require("common.Util")
local MoshouLogic = require("moshou.MoshouLogic")
local TequanShopLogic = require("present.TequanShopLogic")
local GuideLogic = require("guide.GuideLogic")
local HeroLogLogic = require("absAct.HeroLogLogic")
local HeroGrowUp = require("absAct.HeroGrowUp")
local MengxinLogic = require("present.MengxinLogic")
local YunYingLogic = require("yunying.YunYingLogic")


DEFAULT_BASE_TASK_CNT = 6         -- 默认基本任务数
DEFAULT_REFRESH_ZUANSHI_CNT = 30  -- 默认刷新所需钻石数量
DEFAULT_MAX_TASK_CNT = 200        -- 最大的任务数
DEFAULT_MAX_DOUBLE_CNT = 10      -- 双倍领取次数

BARTASK_OP_1 = 1 -- 放弃任务
BARTASK_OP_2 = 2 -- 刷新任务
BARTASK_OP_3 = 3 -- 开始任务
BARTASK_OP_4 = 4 -- 完成/加速任务

BARTASK_DAY_FREE = 2  -- 玩家每日免费刷新次数
BARTASK_QUICK_FINISH_TIME = 60 * 60  -- 玩家免费完成时间
BARTASK_TASK_NEED_ISR = 2200   -- 接取任务所需要的情报

GUIDE_DEFAUT_STAR = {[1] = 4}
GUIDE_DEFAUT_ITEM = {[1]= {ItemDefine.ITEM_DRAWCARD_GAOCHOU_ID, 1}}


function setBarTaskData(msgData, taskID, human, dbData, now)
    if type(taskID) ~= "number" then
        return
    end

    if now == nil then
        now = os.time()
    end

    msgData.taskID = taskID
    local heroConfig = HeroExcel.hero[dbData.nameID]
    local name = heroConfig.name
    local tempConfig = BarTaskExcel.taskName[dbData.star]
    local action = nil

    if dbData.star <= 3 then
        action = tempConfig.actionName[dbData.actionID]
        msgData.name = Util.format(Lang.BAR_NAME_FORMAT, action, name)
    else
        action = tempConfig.taskName[dbData.actionID]
        msgData.name = Util.format(Lang.BAR_NAME_FORMAT, name, action)
    end

    msgData.star = dbData.star

    -- 存储的时间是：领取任务时间 - 1
    if dbData.ts then
        msgData.leftTime = BarTaskExcel.barTask[dbData.star].useTime -(now - dbData.ts)
        if msgData.leftTime < 0 then
            msgData.leftTime = 0
        end
    else
        msgData.leftTime = BarTaskExcel.barTask[dbData.star].useTime
    end
    msgData.needTime = BarTaskExcel.barTask[dbData.star].useTime
 --   local itemID = dbData.itemID
 --   local itemCnt = dbData.itemCnt
  --  Grid.makeItem(msgData.item, itemID, itemCnt)

    local need = getQuickNeed(msgData.leftTime)
    msgData.zuanshiNeed = need
    -- BarTaskExcel.barTask[dbData.star].zuanshiNeed

    msgData.heroIndex[0] = 0
    if dbData.hero then
        msgData.heroIndex[0] = #dbData.hero
        for i = 1, #dbData.hero do
            msgData.heroIndex[i] = dbData.hero[i]
        end
    end
end
    
function rewardQuery(human, star)
    local barTaskConfig = BarTaskExcel.barTask[star]

    local msgRet = Msg.gc.GC_BARTASK_REWARD_QUERY
    msgRet.star = star
    for k, v in ipairs(barTaskConfig.reward) do
        Grid.makeItem(msgRet.item[k], v[1],  v[2][1])
    end
    msgRet.item[0] = #barTaskConfig.reward
    Msg.send(msgRet, human.fd)
end

-- 查询酒馆任务
function query(human)
    if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1104, true) then
        return
    end
    ObjHuman.updateDaily(human)
    initDB(human)
    guideBar(human)
    local needIsr = BARTASK_TASK_NEED_ISR
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_5)
    if cjPrivilege then
        local rate =(100 - cjPrivilege) / 100
        needIsr = needIsr * rate
    end
    local msgRet = Msg.gc.GC_BARTASK_QUERY
    local now = os.time()
    local ts1 = Util.getDayStartTime(now)
    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    msgRet.refreshTime = 24 * 60 * 60 -(now - ts1)
    msgRet.refreshNeed = DEFAULT_REFRESH_ZUANSHI_CNT
    msgRet.qingbaoNeed = needIsr
    msgRet.nowQingBao = human.db.bar and human.db.bar.qingbao or 0
    msgRet.qingBaoMax = RoleExcel.exp[human.db.lv].qingBaoMax
    msgRet.tequan = human.db.bar and human.db.bar.vip or 0
    msgRet.tequanSuper = human.db.bar and human.db.bar.superVip or 0
    msgRet.freeCnt = BARTASK_DAY_FREE - human.db.bar.refresCnt
    msgRet.freeCnt = msgRet.freeCnt > 0 and msgRet.freeCnt or 0
    msgRet.doubleCnt = 0
    Grid.makeItem(msgRet.refresItem, ItemDefine.ITEM_BASE_JUANZHOU_ID, 1)

    if double and human.db.bar.doubleCnt <= DEFAULT_MAX_DOUBLE_CNT then
        msgRet.doubleCnt = DEFAULT_MAX_DOUBLE_CNT - human.db.bar.doubleCnt 
    end
    local cnt = 0
    for key, value in pairs(human.db.bar) do
        if type(key) == "number" then
            cnt = cnt + 1

            setBarTaskData(msgRet.list[cnt], key, human, value, now)

            if cnt >= 200 then
                break
            end
        end
    end
    
    msgRet.list[0] = cnt
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 刷新酒吧任务
function refresh(human)
    ObjHuman.updateDaily(human)
    initDB(human)

    local allTaskCnt = getAllTaskCnt(human)
    if allTaskCnt >= DEFAULT_MAX_TASK_CNT then
        return Broadcast.sendErr(human, Lang.BAR_TASK_ALL_MAX_ERR)
    end

    local isFree = false
    if human.db.bar.refresCnt < BARTASK_DAY_FREE then
        isFree = true
    end

    -- 判断消耗
    if not isFree then
        -- 检测 是否拥有道具

        if BagLogic.getItemCnt(human, ItemDefine.ITEM_BASE_JUANZHOU_ID) > 0 then
            BagLogic.delItem(human, ItemDefine.ITEM_BASE_JUANZHOU_ID, 1, "bar_refresh")
        else
            local needZuanshi = DEFAULT_REFRESH_ZUANSHI_CNT
            if not ObjHuman.checkRMB(human, needZuanshi) then
                return
            end

            -- 扣消耗
            ObjHuman.decZuanshi(human, - needZuanshi, "bar_refresh")
        end

    end

    human.db.bar.refresCnt = human.db.bar.refresCnt + 1

    -- 改db
    for taskID, taskData in pairs(human.db.bar) do
        if type(taskID) == "number" then
            if taskData.isLock == nil then
                human.db.bar[taskID] = nil
            end
        end
    end
    addRandomTask(human, DEFAULT_BASE_TASK_CNT)
    query(human)
end

-- 查看具体任务信息
function getQuery(human, taskID)
    ObjHuman.updateDaily(human)
    initDB(human)

    local taskData = human.db.bar[taskID]
    if taskData == nil then
        return
    end

    local msgRet = Msg.gc.GC_BARTASK_GET_QUERY
    local barTaskConfig = BarTaskExcel.barTask[taskData.star]
    msgRet.star = barTaskConfig.starNeed[1] or 0

    msgRet.camp[0] = taskData.camp and #taskData.camp or 0
    for i = 1, #taskData.camp do
        msgRet.camp[i] = taskData.camp[i]
    end
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 新手引导下默认高抽
function guideBar(human)
    if not human.db.bar or not human.db.bar[1] then return end
    local flag = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_BARTASK)
    if flag then return end

    human.db.bar[1].star = GUIDE_DEFAUT_STAR[1]
 --   human.db.bar[1].itemID = GUIDE_DEFAUT_ITEM[1][1]
 --   human.db.bar[1].itemCnt = GUIDE_DEFAUT_ITEM[1][2]
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BARTASK)
end


-- 领取任务
function getDo(human, taskID, heroIndexList, heroIDList)
    if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1104, true) then
        return
    end
    ObjHuman.updateDaily(human)
    initDB(human)

    guideBar(human)

    local taskData = human.db.bar[taskID]
    if taskData == nil then
        return
    end


    local barTaskConfig = BarTaskExcel.barTask[taskData.star]
    -- 判断数量
    if heroIDList[0] ~= heroIndexList[0] then
        return
    end

    local needIsr = BARTASK_TASK_NEED_ISR
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_5)
    if cjPrivilege then
        local rate =(100 - cjPrivilege) / 100
        needIsr = needIsr * rate
    end
    -- 判定情报值
    if human.db.bar.qingbao < needIsr then
        return Broadcast.sendErr(human, Lang.BAR_TASK_GET_ERR_ISR)
    end

    local needStar = barTaskConfig.starNeed[1]
    local needStarCnt = barTaskConfig.starNeed[2]
    local nowStarCnt = 0
    local camp2Cnt = { }

    -- 判断英雄
    local indexTable = { }
    for i = 1, heroIDList[0] do
        local tempIndex = heroIndexList[i]
        local heroGird = human.db.heroBag[tempIndex]
        if type(heroGird) ~= "table" then
            -- 没有这个英雄
            return
        end
        if heroGird.id ~= heroIDList[i] then
            -- 没对应上
            return
        end
        if indexTable[tempIndex] then
            -- 发了重复id
            return
        end
        if isSelectForBarTask(human, tempIndex) then
            -- 已经在其它悬赏中选择了
            return
        end
        indexTable[tempIndex] = true
        local heroConfig = HeroExcel.hero[heroGird.id]
        if heroGird.star >= needStar then
            nowStarCnt = nowStarCnt + 1
        end
        camp2Cnt[heroConfig.camp] =(camp2Cnt[heroConfig.camp] or 0) + 1
    end
    -- 判断条件	
    if nowStarCnt < needStarCnt then
        return Broadcast.sendErr(human, Util.format(Lang.BAR_TASK_GET_ERR_STAR, needStar, needStarCnt))
    end
    local campLen = taskData.camp and #taskData.camp or 0
    for i = 1, campLen do
        local needCamp = taskData.camp[i]
        local nowCampCnt = camp2Cnt[needCamp] or 0
        if nowCampCnt < 1 then
            return Broadcast.sendErr(human, Lang.BAR_TASK_GET_ERR_CAMP)
        end
        camp2Cnt[needCamp] = nowCampCnt - 1
    end

    -- 扣除情报点
    human.db.bar.qingbao = human.db.bar.qingbao - needIsr

    -- 改db
    human.db.bar[taskID].isLock = true
    -- 因为：领取任务后，马上就去query查询，间隔太短
    -- 导致：时间间隔为0，此时剩下的时间和需要花费的时间相等，导致客户端无法判断
    -- 所以：领取时间要减去1秒，用来区分
    -- 链接至：query(human)查看
    human.db.bar[taskID].ts = os.time() -1
    human.db.bar[taskID].hero = { }
    for i = 1, heroIDList[0] do
        human.db.bar[taskID].hero[i] = heroIndexList[i]
    end

    -- 通知客户端
    sendBarTaskUpdate(human, taskID, BARTASK_OP_3)

    DailyTaskLogic.recordDailyTaskFinishCnt(human, DailyTaskLogic.DAILY_TASK_ID_9, 1)
    ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_7, 1)
    MengxinLogic.onCallBack(human,MengxinLogic.MX_TASK_TYPE_4,1)
    if taskData.star == 6 then
        ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_18, 1)
	    HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE17, 1)
    end
    if taskData.star >= 5 then
        HeroLogLogic.finishTaskCB(human,HeroLogLogic.HERO_LOG_TYPE_4,1)
    end

    YunYingLogic.onCallBack(human, "onBarTaskgetDo",1)
end

function getQuickNeed(surTime)
    for k, config in ipairs(BarTaskExcel.accTime) do
        if surTime < config.maxTime then
            return config.zuanshiNeed
        end
    end
    return BarTaskExcel.accTime[#BarTaskExcel.accTime].zuanshiNeed
end

-- 加速完成任务
function quickFinish(human, taskID)
    ObjHuman.updateDaily(human)
    initDB(human)

    local taskData = human.db.bar[taskID]
    if taskData == nil then
        return
    end

    -- 条件判断
    if taskData.isLock == nil or taskData.ts == nil then
        return
    end

    -- 消耗判断
    local barTaskConfig = BarTaskExcel.barTask[taskData.star]
    if not barTaskConfig then return end

    local surTime = barTaskConfig.useTime -(os.time() - taskData.ts)
    local needZuanshi = getQuickNeed(surTime)

    if needZuanshi > 0 then
        if not ObjHuman.checkRMB(human, needZuanshi) then
            return
        end

        -- 扣消耗
        ObjHuman.decZuanshi(human, - needZuanshi, "bar_quick_finish")
    end

    -- 改db
    human.db.bar[taskID].ts = 0

    finishTask(human, taskID)
end

function randomTak(human, taskID)
    
    local taskData = human.db.bar[taskID]
    if taskData.random then return end

    local barTaskConfig = BarTaskExcel.barTask[taskData.star]
    if not barTaskConfig then return end

    local totalWeight = 0
    for i = 1, #barTaskConfig.reward do
        totalWeight = totalWeight + barTaskConfig.reward[i][3]
    end

    local getList = {}
    local getRare = 0
    for i = 1 , 3 do 
        local random = math.random(1, totalWeight - getRare)
        for k, v in pairs(barTaskConfig.reward) do
            if not getList[k] and random <= v[3] then
                getList[k] = i
                getRare = getRare + v[3]
                break
            elseif not getList[k] then  
                -- 防止循环最后一个是 已经随机过的  导致 随机不出来
                random = random - v[3]
            end
        end
    end    

    taskData.random = {}
    for k, v in pairs(getList) do
        local config = barTaskConfig.reward[k]
        taskData.random[v] = {}
        taskData.random[v].itemID = config[1]
        taskData.random[v].itemCnt = math.random(config[2][1], config[2][2] )
    end
end

-- 提交任务
function finishTask(human, taskID)
    ObjHuman.updateDaily(human)
    initDB(human)
    local taskData = human.db.bar[taskID]
    if taskData == nil then
        return
    end

    -- 判断条件
    local star = taskData.star
    local needTime = BarTaskExcel.barTask[star].useTime
    local now = os.time()
    if now - taskData.ts < needTime then
        return
    end

    if not human.db.bar[taskID].random then
        randomTak(human, taskID)
    end

   

    -- 双倍参数
    --[[
         local itemID = taskData.itemID
    local itemCnt = taskData.itemCnt
 local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    local isDouble = false
    if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
        itemCnt = itemCnt * 2
        human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
        isDouble = true
    end
    ]]
   

    -- 改db
--    human.db.bar[taskID] = nil
    -- 给奖励
  --  BagLogic.addItem(human, itemID, itemCnt, "bar_reward")
    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    local isDouble = false
    local beishu = 1
    if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
        isDouble = true
        beishu = 2
    end
    -- 通知客户端
    local msgRet = Msg.gc.GC_BARTASK_FINISH_TASK
    msgRet.taskID = taskID
    msgRet.double = isDouble and 1 or 0
    
    for k, v in ipairs(human.db.bar[taskID].random) do
        Grid.makeItem(msgRet.item[k], v.itemID, v.itemCnt * beishu)    
    end
    msgRet.item[0] = #human.db.bar[taskID].random
    Msg.send(msgRet, human.fd)

   -- MoshouLogic.onCallBack(human, MoshouLogic.MOSHOU_TASKTYPE_XUANSHANG, 1)
	HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE15, 1)

end

function chooseReward(human, taskID, index)
    ObjHuman.updateDaily(human)
    initDB(human)
    local taskData = human.db.bar[taskID]
    if taskData == nil then
        return
    end

    if not human.db.bar[taskID].random or not human.db.bar[taskID].random[index] then
        return
    end

    local itemID = human.db.bar[taskID].random[index].itemID
    local itemCnt = human.db.bar[taskID].random[index].itemCnt
    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    local isDouble = false
    if taskData.star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
        itemCnt = itemCnt * 2
        human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
        isDouble = true
    end

      -- 改db
    human.db.bar[taskID] = nil
    -- 给奖励
    BagLogic.addItem(human, itemID, itemCnt, "bar_reward")

    local msgRet = Msg.gc.GC_BARTASK_CHOOSE_REWARD
    msgRet.taskID = taskID
    msgRet.double = isDouble and 1 or 0
    Grid.makeItem(msgRet.item, itemID, itemCnt)    
    Msg.send(msgRet, human.fd)

end

-- 一键领取
function CG_BARTASK_FINISH_ALL(human)
    do return end

    if not human.db.bar then return end

    -- 双倍参数 -- 清空次数
    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    local isDouble 
    local msgRet = Msg.gc.GC_BARTASK_FINISH_ALL
   
    local list = nil
    local cnt = 0
    for taskID, taskData in pairs(human.db.bar) do
        if type(taskID) == "number" and taskData.ts then
            local star = taskData.star
            local needTime = BarTaskExcel.barTask[star].useTime
            local now = os.time()
            if now - taskData.ts >= needTime then
                local itemID = taskData.itemID
                local itemCnt = taskData.itemCnt
                -- 计算双倍
                if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
                    itemCnt = itemCnt * 2
                    human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
                    isDouble = true
                end
                list = list or { }
                list[itemID] =(list[itemID] or 0) + itemCnt
                human.db.bar[taskID] = nil
                cnt = cnt + 1
                BagLogic.addItem(human, itemID, itemCnt, "bar_reward")
            end
        end
    end

    if not list then return end
    msgRet.double = isDouble and 1 or 0
    msgRet.items[0] = 0
    for itemID, itemCnt in pairs(list) do
        msgRet.items[0] = msgRet.items[0] + 1
        if not msgRet.items[msgRet.items[0]] then
            msgRet.items[msgRet.items[0]] = Util.copyTable(msgRet.items[msgRet.items[0] -1])
        end
        Grid.makeItem(msgRet.items[msgRet.items[0]], itemID, itemCnt)
    end

    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)

    query(human)

end

function heroIndexByDelForPos(human, index)
    if not human.db.bar then return end

    for taskID, v in pairs(human.db.bar) do
        if type(taskID) == "number" then
            local heroLen = v.hero and #v.hero or 0
            for i = 1, heroLen do
                if v.hero[i] == index then
                    table.remove(v.hero, i)
                    break
                end
            end
        end
    end
end

-- 是否已上阵
function isSelectForBarTask(human, index)
    if not human.db.bar then return end

    for taskID, v in pairs(human.db.bar) do
        if type(taskID) == "number" then
            local heroLen = v.hero and #v.hero or 0
            for i = 1, heroLen do
                if v.hero[i] == index then
                    return true
                end
            end
        end
    end
end

-- 激活酒馆悬赏 特权
function activateBarVip(human, type)
    initDB(human)

    if type == 1 then
        human.db.bar.vip = 1
    elseif type == 2 then
        human.db.bar.superVip = 1
    end
end

------------------------------功能函数-------------------------------------------
function getEmptyIndex(human)
    for index = 1, 1000 do
        if human.db.bar[index] == nil then
            return index
        end
    end
end

-- 初始化酒馆任务数据
function initDB(human)
    if human.db.bar ~= nil then
        return
    end

    human.db.bar = { }
    human.db.bar.refresCnt = 0
    human.db.bar.qingbao = 0
    human.db.bar.doubleCnt = 0 -- 双倍次数
    local newAddCnt = getBaseTaskCnt(human)
    addRandomTask(human, newAddCnt, {4}, true)
end

-- 跨天刷新酒馆任务
function refreshDailyTask(human)
    if human.db.bar == nil then
        return
    end

    human.db.bar.refresCnt = 0
    human.db.bar.doubleCnt = 0 -- 双倍次数
end

-- 获取现在的任务总数
function getAllTaskCnt(human)
    if human.db.bar == nil then
        return 0
    end

    local allTaskCnt = 0
    for key, value in pairs(human.db.bar) do
        if value ~= nil then
            allTaskCnt = allTaskCnt + 1
        end
    end
    return allTaskCnt
end

-- 获取玩家基础任务数
function getBaseTaskCnt(human)
    return DEFAULT_BASE_TASK_CNT
end

--   随机 悬赏任务    starList[第几个] = 星级    itemList[第几个] = {itemID, itemCnt}
function addRandomTask(human, cnt, starList, isInit)
    if cnt < 1 then return end

    -- 检查是否 已经达到上限
    local allTaskCnt = getAllTaskCnt(human)
    if allTaskCnt >= 200 then
        return
    end

    local star = nil
    local camp = nil
    local nameID = nil
    local actionID = nil

    local vipChange = 0
    if (human.db.bar.refresCnt == BARTASK_DAY_FREE + 1 and human.db.bar.vip) or(human.db.bar.refresCnt == BARTASK_DAY_FREE + 3 and human.db.bar.superVip) then
        vipChange = math.random(1, cnt)
    end
    for i = 1, cnt do
        star = getRandomStar(human)

        if vipChange > 0 and i == vipChange then
            if human.db.bar.refresCnt == BARTASK_DAY_FREE + 1 then
                star = 4
            else
                star = 5
            end
        end
         
        if starList and starList[i] then
            star = starList[i]
        end
        itemID, itemCnt = getRandomItem(human, star)        
        nameID, actionID = getRandomName(human, star)
        camp = getRandomCond(star)

        if isInit then
            camp[1] = 1
            camp[2] = 2
        end
        local emptyIndex = getEmptyIndex(human)
        if emptyIndex then
            human.db.bar[emptyIndex] = { }
            human.db.bar[emptyIndex].star = star
            human.db.bar[emptyIndex].nameID = nameID
            human.db.bar[emptyIndex].actionID = actionID
            human.db.bar[emptyIndex].camp = camp
            human.db.bar[emptyIndex].isLock = nil
            human.db.bar[emptyIndex].ts = nil
            human.db.bar[emptyIndex].hero = nil
        end
    end
end

-- 添加情报值
function addBarQingBao(human, cnt)
    initDB(human)
    if human.db.bar == nil then
        return
    end

    local max = RoleExcel.exp[human.db.lv].qingBaoMax

    if human.db.bar.qingbao + cnt < max then
        human.db.bar.qingbao = human.db.bar.qingbao + cnt
    else
        human.db.bar.qingbao = max
    end
end

function getRandomStar(human)
    local barTaskConfig = BarTaskExcel.barTask
    local totalWeight = 0
    for i = 1, #barTaskConfig do
        totalWeight = totalWeight + barTaskConfig[i].chance
    end

    local randNum = math.random(1, totalWeight)

    local star = 0
    for j = 1, #barTaskConfig do
        local tempWeight = barTaskConfig[j].chance
        if randNum <= tempWeight then
            star = j
            break
        end
        randNum = randNum - tempWeight
    end

    return star
end

function getRandomItem(human, star)
    local barTaskConfig = BarTaskExcel.barTask[star]
    if not barTaskConfig then return end

    local totalWeight = 0
    for i = 1, #barTaskConfig.reward do
        totalWeight = totalWeight + barTaskConfig.reward[i][3]
    end

    local randNum = math.random(1, totalWeight)

    local itemID = 0
    local itemCnt = 0
    for j = 1, #barTaskConfig.reward do
        local tempWeight = barTaskConfig.reward[j][3]
        if randNum <= tempWeight then
            itemID = barTaskConfig.reward[j][1]
            local minCnt = barTaskConfig.reward[j][2][1]
            local maxCnt = barTaskConfig.reward[j][2][2]
            itemCnt = math.random(minCnt, maxCnt)
            break
        end
        randNum = randNum - tempWeight
    end

    return itemID, itemCnt
end

function getRandomName(human, star)
    local barTaskConfig = BarTaskExcel.taskName[star]
    if not barTaskConfig then return end

    local randNum = math.random(1, #barTaskConfig.heroName)
    local nameID = barTaskConfig.heroName[randNum]

    local actionID = nil
    if star <= 3 then
        actionID = math.random(1, #barTaskConfig.actionName)
    else
        actionID = math.random(1, #barTaskConfig.taskName)
    end
    return nameID, actionID
end

function getRandomCond(star)
    local barTaskConfig = BarTaskExcel.barTask[star]
    if not barTaskConfig then return end

    local heroCnt = barTaskConfig.heroCnt
    local camp = { }
    local c = 0
    local j = 0

    for i = 1, 5 do camp[i] = i end
    for i = 1, heroCnt do
        c = c + 1
        local r = math.random(c, #camp)
        camp[c], camp[r] = camp[r], camp[c]
    end
    for i = #camp, c + 1, -1 do
        camp[i] = nil
    end
    return camp
end

-- 下发酒馆任务更新
function sendBarTaskUpdate(human, taskID, op)
    local msgRet = Msg.gc.GC_BARTASK_UPDATE
    msgRet.op = op
    setBarTaskData(msgRet.task, taskID, human, human.db.bar[taskID])
    msgRet.nowQingBao = human.db.bar and human.db.bar.qingbao or 0
    Msg.send(msgRet, human.fd)
end

-- 是否有红点
function isDot(human)
    if human.db.bar == nil then
        return false
    end
    local now = os.time()
    for k, v in pairs(human.db.bar) do
        if type(k) == "number" then
            if v.ts == nil then
                return false
            end

            if v.isLock and v.ts then
                local needTime = BarTaskExcel.barTask[v.star].useTime
                if now - v.ts > needTime then
                    return true
                end
            end
        end
    end
    return false
end